#pragma once
#include <iostream>
#include "Protocol.hpp"
using namespace std;
enum
{
    DIV_ZERO = 1,
    MOD_ZERO,
    OTHER_OPER
};

class ServerCal
{
public:
    ServerCal()
    {

    }
    static Response CalculatorHelper(const Request& req)
    {
        Response resp(0,0);
        switch(req.op)
        {
        case '+':
            resp.result = req.x + req.y;
            resp.code = 0;
            break;
        case '-':
            resp.result = req.x - req.y;
            resp.code = 0;
            break;
        case '*':
            resp.result = req.x * req.y;
            resp.code = 0;
            break;
        case '/':
            if (req.y == 0)
            {
                resp.result = -1;
                resp.code = DIV_ZERO;
                break;
            }
            resp.result = req.x / req.y;
            resp.code = 0;
            break;
        case '%':
            if (req.y == 0)
            {
                resp.code = MOD_ZERO;
                break;
            }
            resp.result = req.x % req.y;
            resp.code = 0;
            break;
        default:
            resp.code = OTHER_OPER;
            break;
        }
        return resp;
    }
    static std::string Calculator(std::string &package)
    {
        std::string content;
        bool r = Decode(package, &content);
        if(!r) return "";
        Request req;
        r = req.Deserialize(content);
        if(!r) return "";
        
        content = "";
        Response resp = CalculatorHelper(req);
        resp.Serialize(&content);
        content = Encode(content);
        return content;
    }
    ~ServerCal()
    {}
};